package cn.hotel.service;

import cn.hotel.mapper.MemberDao;
import cn.hotel.mapper.MemberDaoImpl;
import cn.hotel.pojo.Member;
import cn.hotel.utlis.ResultMapUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MemberServiceImpl implements MemberService{

    private MemberDao memberDao = new MemberDaoImpl();
    @Override
    public Map<String, Object> login(String account, String password) {
        Member member = memberDao.login(account);
        if (password!=null && member != null && password.equals(member.getPassword())){
            Map<String,Object> resultSet = ResultMapUtil.success("登录成功");
            resultSet.put("data",member);
            //登录成功
            return resultSet;



        }
        return ResultMapUtil.error("登录失败！");
    }

    @Override
    public Map<String, Object> register(Member member) {
        return member!=null && memberDao.register(member)>0 ?
                ResultMapUtil.success("注册成功")
                : ResultMapUtil.error("注册失败");
    }

    @Override
    public List<Member> getMemberList(String account, int currentIndex, int pageSize) {
        //根据当前页计算起始位置
        int index = (currentIndex - 1) * pageSize;
        List<Member> memberList = memberDao.getMemberList(account,index,pageSize);

        return memberList;
    }

    @Override
    public int getCount(String account) {
        int count = 0;
        try {
            count = memberDao.getCount(account);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public Map<String, Object> deleteMember(String account) {
        Map<String ,Object> members = new HashMap<>();

        try {
            int num = memberDao.deleteMember(account);
            if(num > 0){
                members.put("flag","1");
                members.put("mag","删除成功");
            }else {
                members.put("flag","0");
                members.put("mag","删除失败");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            members.put("flag","-1");
            members.put("mag","程序异常");
        }
        return members;
    }




}
